{include file="common/head"/}
<div class="admin-main layui-anim layui-anim-upbit">
    <fieldset class="layui-elem-field layui-field-title">
        <legend>{$title}</legend>
    </fieldset>
    <form class="layui-form layui-form-pane" lay-filter="form">
        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 150px">传感器编码</label>
            <div class="layui-input-4">
                <input type="text" name="sensor_mac" lay-verify="required" placeholder="请输入传感器编码" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 150px">传感器名称</label>
            <div class="layui-input-4">
                <input type="text" name="sensor_name" lay-verify="required" placeholder="请输入传感器类型名称"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item" >
            <label class="layui-form-label" style="width: 150px">大棚</label>
            <div class="layui-input-4">
                <select name="house_id" lay-verify="required"  lay-filter="hid">
                    <option value="">选择大棚</option>
                    {volist  name='house' id='vo'}
                    <option value="{$vo.id}">{$vo.house_name}</option>
                    {/volist}
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 150px">选择网关</label>
            <div class="layui-input-4">
                <select name="gateway_id" lay-verify="required" id="gateway_id" >
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 150px">选择传感器类型</label>
            <div class="layui-input-4">
                <select name="type_id">
                    <option value="">选择传感器类型</option>
                    {foreach name="res" item="vo"}
                    <option value="{$vo.id}">{$vo.name}</option>
                    {/foreach}
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">

                <button type="button" class="layui-btn" lay-submit="" lay-filter="submit">{:lang('submit')}</button>
                <a onclick="navigate_back()" class="layui-btn layui-btn-primary">{:lang('back')}</a>
            </div>
        </div>
    </form>
</div>
{include file="common/foot"/}
<script>
    layui.use(['form', 'layer','upload','element'], function () {
        var form = layui.form,layer = layui.layer,upload = layui.upload,$ = layui.jquery,element = layui.element;
        //提交监听
        form.on('select(hid)', function (data) {
            $.ajax({
                url:'{:url("SensorAdd")}',
                type:'post',
                data:{'house_id':data.value},
                success:function(res){
                    var opt = '';
                    if(res.length > 0){
                        for(var i = 0; i < res.length; i++){
                            opt +='<option value="'+res[i]['id']+'">'+res[i]['gateway']+'</option>';
                        }
                    }else {
                        opt ='<option >暂无网关</option>';
                    }
                    $("#gateway_id").html(opt);
                    form.render('select');
                }
            });
        });
        form.on('submit(submit)', function (data) {
            loading = layer.load(1, {shade: [0.1, '#fff']});
            $.post('{:url("SensorAdd")}', {sensor_mac:data.field.sensor_mac,sensor_name :data.field.sensor_name,gateway_id:data.field.gateway_id,type_id:data.field.type_id}, function (res) {
                layer.close(loading);
                if (res.code > 0) {
                    layer.msg(res.msg, {time: 1800, icon: 1}, function () {
                        window.parent.location.reload();
                    });
                } else {
                    layer.msg(res.msg, {time: 1800, icon: 2});
                }
            });
        });
    });

    function navigate_back() {
        //parent.location.href="index";
        parent.layer.closeAll();
    }
</script>